package com.lcj.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.lcj.bff.driver.controller.form.DeleteCosFileForm;
import com.lcj.common.exception.AochException;
import com.lcj.common.util.CosUtil;
import com.lcj.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author : lcj
 * @create 2022/11/5 19:41
 */
@RestController
@RequestMapping("/cos")
@Slf4j
@Tag(name = "CosController", description = "对象存储Web接口")
public class CosController {

    @Resource
    private CosUtil cosUtil;

    @PostMapping("/uploadCosPublicFile")
    @SaCheckLogin
    @Operation(summary = "上传文件")
    public Result uploadCosPublicFile(@Param("file") MultipartFile file, @Param("module") String module) {
        if (file.isEmpty()) {
            throw new AochException("上传文件不能为空");
        }
        try {
            String path = null;
            //TODO 此处应该有path路径赋值

            HashMap map = cosUtil.uploadFile(CosUtil.PUBLIC, file, path);
            return Result.ok(map);
        } catch (IOException e) {
            log.error("文件上传到腾讯云错误", e);
            throw new AochException("文件上传到腾讯云错误");
        }
    }

    @PostMapping("/uploadCosPrivateFile")
    @SaCheckLogin
    @Operation(summary = "上传文件")
    public Result uploadCosPrivateFile(@Param("file") MultipartFile file, String module) {
        if(file.isEmpty()) {
            throw new AochException("上传文件不能为空");
        }
        try {
            String path = null;
            if("driverAuth".equals(module)) {
                path = "/driver/auth/";
            } else {
                throw new AochException("module错误");
            }
            HashMap map = cosUtil.uploadFile(CosUtil.PRIVATE, file, path);
            return Result.ok(map);
        } catch (Exception e) {
            log.error("文件上传到服务器错误", e);
            throw new AochException("文件上传到服务器错误");
        }
    }

    /**
     * 删除公有存储桶上的文件
     * @param form
     * @return
     */
    @PostMapping("/deleteCosPublicFile")
    @SaCheckLogin
    @Operation(summary = "删除文件")
    public Result deleteCosPublicFile(@Valid @RequestBody DeleteCosFileForm form) {
        cosUtil.deleteFile(CosUtil.PUBLIC ,form.getPaths());
        return Result.ok();
    }

    /**
     * 删除私有存储桶上的文件
     * @param form
     * @return
     */
    @PostMapping("/deleteCosPrivateFile")
    @SaCheckLogin
    @Operation(summary = "删除文件")
    public Result deleteCosPrivateFile(@Valid @RequestBody DeleteCosFileForm form) {
        cosUtil.deleteFile(CosUtil.PRIVATE, form.getPaths());
        return Result.ok();
    }
}
